package com.ourway.controller;

import cn.hutool.core.util.StrUtil;
import com.liuweiheng.base.entity.R;
import com.ourway.dtos.DepartmentDTO;
import com.ourway.entity.vo.*;
import com.ourway.service.IFeiShuService;
import com.ourway.service.IDepartmentService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 *
 * @author lwh
 * @date 2021/12/03
 */
@RestController
@RequestMapping("feishu/department")
public class FeiShuDepartmentController {

    private final IFeiShuService IFeiShuService;
    private final IDepartmentService departmentService;

    public FeiShuDepartmentController(IFeiShuService IFeiShuService, IDepartmentService departmentService) {
        this.IFeiShuService = IFeiShuService;
        this.departmentService = departmentService;
    }

    /**
     * 获取所有子群组
     */
    @GetMapping("/getAllDepartment")
    public R getAllDepartment() {
        FeiShuSearchChildDeptVo response = IFeiShuService.getAllDepartment();
        return R.ok().putData(response);
    }

    /**
     * 获取部门下所有用户
     */
    @GetMapping("/getAllUserByDepartment")
    public R getAllUserByDepartment() throws JSONException {
        FeiShuSearchUserByDeptResponseVo response = IFeiShuService.getAllUserByDepartment(null);
        return R.ok().putData(response);
    }

    /**
     * 创建部门
     * @param param 部门信息
     */
    @PostMapping("/createDept")
    public R createDept(@RequestBody DepartmentDTO param) throws JSONException {
        if (null == param) {
            return R.error("Bad Request, Illegal Argument");
        }
        if (!departmentService.creteDept(param)) {
            return R.error("创建部门出错");
        }
        return R.ok();
    }

    /**
     * 获取部门树
     * @return 树结构数据
     */
    @GetMapping("/getTree")
    public DepartmentDTO getTree() {
        return departmentService.getTree();
    }

    /**
     * 获取分页部门列表
     * @param pageQueryVo 分页信息
     * @return 部门列表
     */
    @PostMapping("/getDeptList")
    public PageResultDto<DepartmentDTO> getDepartmentList(@RequestBody PageQueryVo pageQueryVo) {
        int pageIndex = pageQueryVo.getPageIndex();
        int pageSize = pageQueryVo.getPageSize();
        return departmentService.getDeptList(pageIndex, pageSize);
    }

    @PostMapping("/update")
    public R update(@RequestBody DepartmentDTO departmentDTO) {
        if (null == departmentDTO || StrUtil.isBlank(departmentDTO.getDepartmentId())) {
            return R.error("Bad Request! RequestParam Is Empty");
        }
        if (!departmentService.update(departmentDTO)) {
            return R.error("更新失败");
        }
        return R.ok("更新成功");
    }

    @PostMapping("/delete")
    public R delete(@RequestBody DepartmentDTO departmentDTO) {
        if (null == departmentDTO || StrUtil.isBlank(departmentDTO.getDepartmentId())) {
            return R.error("Bad Request! RequestParam Is Empty");
        }
        if (!departmentService.delete(departmentDTO)) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }
}
